package com.feeyin.radar.commons;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class Page
{
  private int pageStartRow = 0;
  private int pageEndRow = 0;
  private int totalPages = 0;

  public int getPageStartRow()
  {
    return this.pageStartRow;
  }

  public void setPageStartRow(int pageStartRow) {
    this.pageStartRow = pageStartRow;
  }

  public int getPageEndRow() {
    return this.pageEndRow;
  }

  public void setPageEndRow(int pageEndRow) {
    this.pageEndRow = pageEndRow;
  }

  public void getPageList(int totalRows, Integer pageSize, HttpServletRequest request)
  {
    if (totalRows == 0) {
      request.setAttribute("totalRows", Integer.valueOf(0));
      request.setAttribute("totalPages", Integer.valueOf(0));
      return;
    }

    int currentPage = 1;

    int flag = 1;

    if (pageSize == null) {
      pageSize = Integer.valueOf(18);
    }

    this.pageEndRow = pageSize.intValue();

    if (totalRows % pageSize.intValue() == 0)
      this.totalPages = (totalRows / pageSize.intValue());
    else {
      this.totalPages = (totalRows / pageSize.intValue() + 1);
    }

    if (request.getParameter("flag") == null) {
      if (request.getParameter("cp") != null) {
        currentPage = Integer.parseInt(request.getParameter("cp"));

        this.pageStartRow = ((currentPage - 1) * pageSize.intValue());
      } else {
        currentPage = 1;
      }
    }
    else {
      String inte = (String)request.getSession().getAttribute("currentPage");
      currentPage = Integer.parseInt(inte);
      flag = Integer.parseInt(request.getParameter("flag"));
      if (flag == 1) {
        currentPage = 1;
        this.pageStartRow = 0;
      }

      if (flag == 2) {
        if (currentPage > 1) {
          currentPage--;
          this.pageStartRow = ((currentPage - 1) * pageSize.intValue());
        } else {
          currentPage = 1;
          this.pageStartRow = 0;
        }
      }

      if (flag == 3) {
        if (currentPage < this.totalPages) {
          currentPage++;
          this.pageStartRow = ((currentPage - 1) * pageSize.intValue());
        } else {
          currentPage = this.totalPages;
          this.pageStartRow = ((currentPage - 1) * pageSize.intValue());
        }
      }

      if (flag == 4) {
        currentPage = this.totalPages;
        this.pageStartRow = ((currentPage - 1) * pageSize.intValue());
      }
    }

    request.setAttribute("totalRows", Integer.valueOf(totalRows));
    request.setAttribute("totalPages", Integer.valueOf(this.totalPages));
    request.getSession().setAttribute("currentPage", Integer.toString(currentPage));
  }
}